<template>
  <div class="supplier-add">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑供应商' : '添加供应商' }}</span>
      </div>
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入供应商名称"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'SupplierAdd',
  data() {
    return {
      isEdit: false,
      form: {
        name: '',
        contact: '',
        phone: '',
        address: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    if (id) {
      this.isEdit = true
      this.getDetail(id)
    }
  },
  methods: {
    getDetail(id) {
      // TODO: 调用获取供应商详情接口
      setTimeout(() => {
        this.form = {
          name: '测试供应商',
          contact: '张三',
          phone: '13800138000',
          address: '北京市朝阳区',
          status: 1
        }
      }, 1000)
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // TODO: 调用保存供应商接口
          this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
          this.goBack()
        }
      })
    },
    goBack() {
      this.$router.push({ name: 'product_supplier' })
    }
  }
}
</script>

<style lang="scss" scoped>
.supplier-add {
  .el-form {
    max-width: 600px;
  }
}
</style> 